﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Resources;
using System.Globalization;

namespace JCard
{
    public partial class fGramLearn : Form
    {
        #region Variables for resource
        ResourceManager objResourceManager;
        CultureInfo objCulInfo;
        #endregion

        #region Variables
        // Parent form used when back to Parent form
        fCLesson frmParent;
        bool isLearning;
        ArrayList arr_GramCards;
        ArrayList arr_GramCards_Backup;
        ArrayList arr_GramCards_Wrong;
        string strStatus;
        int intLearned = 0;
        int intRight = 0;
        int total = 0;
        int curGrammar = 0;
        int curExample = 0;


        string strSyntaxClick;
        string strMeanClick;
        string strExampleClick;

        DTO_Grammar curDtoGram;
        #endregion

        #region Constructors
        public fGramLearn()
        {
            InitializeComponent();
        }

        public fGramLearn(ArrayList arr_GramCards, fCLesson frmParent, bool isLearning)
        {
            this.frmParent = frmParent;

            InitializeComponent();

            this.isLearning = isLearning;
            this.arr_GramCards = arr_GramCards;

            SetDisplayLabel();
        }

        private void SetDisplayLabel()
        {
            objResourceManager = new ResourceManager("JCard.Resources", typeof(fCLesson).Assembly);
            objCulInfo = new CultureInfo(Common.GetConfigValue(Constants.CONFIG_LANGUAGE_KEY, Constants.CONFIG_LANGUAGE_VALUE));
        }
        #endregion

        #region Loading
        private void fGramLearn_Load(object sender, EventArgs e)
        {
            if (isLearning)
            {
                btnSai.Visible = true;
                btnDung.Visible = true;
                btnNext.Visible = false;
                btnPrevious.Visible = false;
                btnExampleNxt.Visible = false;
                btnExamplePrev.Visible = false;
                pnlGridExample.Visible = false;

                strStatus = Common.GetResourceValue(Constants.RES_LEARN_RESULT_NAME, 
                    objCulInfo, objResourceManager, Constants.RES_LEARN_RESULT_VALUE);

                strSyntaxClick = Constants.RES_LEARN_SYNTAX_CLICK_VALUE;
                strMeanClick = Constants.RES_LEARN_MEAN_CLICK_VALUE;
                strExampleClick = Constants.RES_LEARN_EXAMPLE_CLICK_VALUE;

                lblSyntax.Click += new EventHandler(lblSyntax_Click);
                lblMeaning.Click += new EventHandler(lblMeaning_Click);
                lblExample.Click += new EventHandler(lblExample_Click);

                total = arr_GramCards.Count;
                arr_GramCards_Wrong = new ArrayList();
                arr_GramCards_Backup = (ArrayList)arr_GramCards.Clone();

                GetRandomGrammar();

                InitDispLearnGram();

                lblStatus.Text = String.Format(strStatus, total, intRight, intLearned);
            }
            else
            {
                btnSai.Visible = false;
                btnDung.Visible = false;
                btnNext.Visible = true;
                btnPrevious.Visible = true;
                btnExampleNxt.Visible = true;
                btnExamplePrev.Visible = true;
                pnlGridExample.Visible = false;

                btnPrevious.Enabled = false;
                btnExamplePrev.Enabled = false;

                if (arr_GramCards.Count == 1)
                {
                    btnNext.Enabled = false;
                }

                strStatus = Common.GetResourceValue(Constants.RES_FAM_RESULT_NAME,
                    objCulInfo, objResourceManager, Constants.RES_FAM_RESULT_VALUE);

                DTO_Grammar dto = (DTO_Grammar)arr_GramCards[curGrammar];

                if (dto.ArrExampleJP.Count <= 1)
                {
                    btnExampleNxt.Enabled = false;
                }

                DisplayGrammarCard(dto);

                lblStatus.Text = String.Format(strStatus, curGrammar + 1, arr_GramCards.Count, curExample + 1, dto.ArrExampleJP.Count);                
            }
        }

        private void DisplayGrammarCard(DTO_Grammar grammar)
        {
            lblSample.Text = grammar.STR_Sample;
            lblSyntax.Text = grammar.STR_Syntax;
            lblMeaning.Text = grammar.GetFullMeaning();

            if (!isLearning)
            {
                lblExample.Text = grammar.GetExample(curExample, true, true);
            }
        }

        private void InitDispLearnGram()
        {
            lblSample.Text = curDtoGram.STR_Sample;
            lblSyntax.TextAlign = ContentAlignment.MiddleCenter;
            lblSyntax.Text = strSyntaxClick;
            lblMeaning.TextAlign = ContentAlignment.MiddleCenter;
            lblMeaning.Text = strMeanClick;
            lblExample.TextAlign = ContentAlignment.MiddleCenter;
            lblExample.Text = strExampleClick;
            pnlGridExample.Visible = false;
            lblExample.Visible = true;

            bool IsScrolled = false;
            if (dtvExample.Rows.Count > 3)
            {
                IsScrolled = true;
            }

            dtvExample.Rows.Clear();
            for (int i = 0; i < curDtoGram.ArrExampleJP.Count; i++)
            {
                dtvExample.Rows.Add(false, curDtoGram.ArrExampleJP[i].ToString(), curDtoGram.ArrExampleVN[i].ToString());
            }

            int delScrollWidth;
            if (dtvExample.Rows.Count > 3)
            {
                if (IsScrolled) delScrollWidth = 0;
                else delScrollWidth = 9;

                ColExamJP.Width -= delScrollWidth;
                ColExamVN.Width -= delScrollWidth;
            }
            else
            {
                if (IsScrolled) delScrollWidth = 9;
                else delScrollWidth = 0;

                ColExamJP.Width += delScrollWidth;
                ColExamVN.Width += delScrollWidth;
            }
        }
        #endregion

        #region Closing
        private void fGramLearn_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmParent.ReShow();
        }
        #endregion

        #region Xử lý Next, Previouse, Next Example, Previous Example
        private void btnNext_Click(object sender, EventArgs e)
        {
            curGrammar++;
            curExample = 0;
            btnExamplePrev.Enabled = false;

            DTO_Grammar dto = (DTO_Grammar)arr_GramCards[curGrammar];
            DisplayGrammarCard(dto);

            lblStatus.Text = String.Format(strStatus, curGrammar + 1, arr_GramCards.Count, curExample + 1, dto.ArrExampleJP.Count);

            if (curGrammar + 1 == arr_GramCards.Count)
            {
                btnNext.Enabled = false;
            }

            if (!btnPrevious.Enabled)
            {
                btnPrevious.Enabled = true;
            }

            if (dto.ArrExampleJP.Count > 0)
            {
                btnExampleNxt.Enabled = true;
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            curGrammar--;
            curExample = 0;
            btnExamplePrev.Enabled = false;

            DTO_Grammar dto = (DTO_Grammar)arr_GramCards[curGrammar];
            DisplayGrammarCard(dto);

            lblStatus.Text = String.Format(strStatus, curGrammar + 1, arr_GramCards.Count, curExample + 1, dto.ArrExampleJP.Count);

            if (curGrammar == 0)
            {
                btnPrevious.Enabled = false;
            }

            if (!btnNext.Enabled)
            {
                btnNext.Enabled = true;
            }

            if (dto.ArrExampleJP.Count > 0)
            {
                btnExampleNxt.Enabled = true;
            }
        }

        private void btnExamplePrev_Click(object sender, EventArgs e)
        {
            curExample--;

            DTO_Grammar dto = (DTO_Grammar)arr_GramCards[curGrammar];
            lblExample.Text = dto.GetExample(curExample, true, true);
            lblStatus.Text = String.Format(strStatus, curGrammar + 1, arr_GramCards.Count, curExample + 1, dto.ArrExampleJP.Count);
            
            if (curExample == 0)
            {
                btnExamplePrev.Enabled = false;
            }

            if (!btnExampleNxt.Enabled)
            {
                btnExampleNxt.Enabled = true;
            }
        }

        private void btnExampleNxt_Click(object sender, EventArgs e)
        {
            curExample++;

            DTO_Grammar dto = (DTO_Grammar)arr_GramCards[curGrammar];
            lblExample.Text = dto.GetExample(curExample, true, true);
            lblStatus.Text = String.Format(strStatus, curGrammar + 1, arr_GramCards.Count, curExample + 1, dto.ArrExampleJP.Count);

            if (curExample + 1 == ((DTO_Grammar)arr_GramCards[curGrammar]).ArrExampleJP.Count)
            {
                btnExampleNxt.Enabled = false;
            }

            if (!btnExamplePrev.Enabled)
            {
                btnExamplePrev.Enabled = true;
            }
        }
        #endregion       

        #region Change Width
        private bool bClick;
        private Point PastPoint;
        private void panel_MouseDown(object sender, MouseEventArgs e)
        {
            bClick = true;
            PastPoint.X = e.X;
            PastPoint.Y = e.Y;
            Invalidate();
        }

        private void panel_MouseUp(object sender, MouseEventArgs e)
        {
            bClick = false;
        }

        private void panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (bClick)
            {
                int delWidth = e.X - PastPoint.X;
                int temp = this.Width + delWidth;
                if (temp >= 4*Constants.MIN_WIDTH && temp <= 2*Constants.MAX_WIDTH)
                {
                    ColExamJP.Width += delWidth / 2;
                    ColExamVN.Width += delWidth - (delWidth / 2);
                    this.Width += delWidth;
                }
            }
        }


        private void panel_MouseMove2(object sender, MouseEventArgs e)
        {
            if (bClick)
            {
                int delWidth = e.X - PastPoint.X;
                int temp = this.Width - delWidth;
                if (temp >= 4*Constants.MIN_WIDTH && temp <= 2*Constants.MAX_WIDTH)
                {
                    this.Left += delWidth;
                    ColExamJP.Width -= delWidth / 2;
                    ColExamVN.Width -= delWidth - (delWidth / 2);
                    this.Width -= delWidth;
                }
            }
        }
        #endregion

        #region Paint Ellipsis Text of Label
        private void Ellipsis_Label_Paint(object sender, PaintEventArgs e)
        {
            Common.DrawEllipsisText((Label)sender, e.Graphics);
        }
        #endregion

        #region Click on Labels
        void lblExample_Click(object sender, EventArgs e)
        {
            lblExample.Visible = false;
            pnlGridExample.Visible = true;          
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            pnlGridExample.Visible = false;
            lblExample.Visible = true;
        }

        void lblMeaning_Click(object sender, EventArgs e)
        {
            if (strMeanClick.Equals(
                lblMeaning.Text))
            {
                lblMeaning.TextAlign = ContentAlignment.MiddleLeft;
                lblMeaning.Text = curDtoGram.GetFullMeaning();
            }
            else
            {
                lblMeaning.TextAlign = ContentAlignment.MiddleCenter;
                lblMeaning.Text = strMeanClick;
            }
        }

        void lblSyntax_Click(object sender, EventArgs e)
        {
            if (strSyntaxClick.Equals(
                lblSyntax.Text))
            {
                lblSyntax.Text = curDtoGram.STR_Syntax;
            }
            else
            {
                lblSyntax.Text = strSyntaxClick;
            }
        }
        #endregion

        #region Dung & Sai
        private void btnSai_Click(object sender, EventArgs e)
        {
            intLearned++;
            lblStatus.Text = String.Format(strStatus, total, intRight, intLearned);
            arr_GramCards_Wrong.Add(curDtoGram);

            if (arr_GramCards.Count > 0)
            {
                GetRandomGrammar();

                InitDispLearnGram();                
            }
            else
            {
                if (Common.ShowConfirmMsg(objCulInfo, objResourceManager,
                            Constants.RES_NOT_FINISH_LEARN_NAME, Constants.RES_NOT_FINISH_LEARN_VALUE,
                            intRight, total - intRight, total - intRight) == DialogResult.Yes)
                {
                    arr_GramCards.AddRange(arr_GramCards_Wrong.ToArray());
                    arr_GramCards_Wrong.Clear();

                    intLearned = 0;
                    intRight = 0;
                    total = arr_GramCards.Count;

                    GetRandomGrammar();

                    InitDispLearnGram();
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void btnDung_Click(object sender, EventArgs e)
        {
            intLearned++;
            intRight++;
            lblStatus.Text = String.Format(strStatus, total, intRight, intLearned);

            if (arr_GramCards.Count > 0)
            {
                GetRandomGrammar();

                InitDispLearnGram();
            }
            else
            {
                if (arr_GramCards_Wrong.Count > 0)
                {
                    if (Common.ShowConfirmMsg(objCulInfo, objResourceManager,
                            Constants.RES_NOT_FINISH_LEARN_NAME, Constants.RES_NOT_FINISH_LEARN_VALUE,
                            intRight, total - intRight, total - intRight) == DialogResult.Yes)
                    {
                        arr_GramCards.AddRange(arr_GramCards_Wrong.ToArray());
                        arr_GramCards_Wrong.Clear();

                        intLearned = 0;
                        intRight = 0;
                        total = arr_GramCards.Count;

                        GetRandomGrammar();

                        InitDispLearnGram();

                        lblStatus.Text = String.Format(strStatus, total, intRight, intLearned);
                    }
                    else
                    {
                        this.Close();
                    }                    
                }
                else
                {
                    if (Common.ShowConfirmMsg(objCulInfo, objResourceManager,
                            Constants.RES_FINISH_LEARN_NAME, Constants.RES_FINISH_LEARN_VALUE) == DialogResult.Yes)
                    {
                        arr_GramCards.AddRange(arr_GramCards_Backup.ToArray());
                        intLearned = 0;
                        intRight = 0;
                        total = arr_GramCards.Count;

                        GetRandomGrammar();

                        InitDispLearnGram();

                        lblStatus.Text = String.Format(strStatus, total, intRight, intLearned);
                    }
                    else
                    {
                        this.Close();
                    }
                }
            }
        }

        private void GetRandomGrammar()
        {
            Random rd = new Random();
            curGrammar = rd.Next(0, arr_GramCards.Count - 1);
            curDtoGram = new DTO_Grammar((DTO_Grammar)arr_GramCards[curGrammar]);
            arr_GramCards.RemoveAt(curGrammar);
        }
        #endregion

        #region Learn Examples
        private void btnLearnEx_Click(object sender, EventArgs e)
        {
            ArrayList arrExJP = new ArrayList();
            ArrayList arrExVN = new ArrayList();

            for (int i = 0; i < dtvExample.Rows.Count; i++)
            {
                DataGridViewRow row = dtvExample.Rows[i];
                bool flag = bool.Parse(row.Cells[0].Value.ToString());
                if (flag)
                {
                    arrExJP.Add(row.Cells[1].Value.ToString());
                    arrExVN.Add(row.Cells[2].Value.ToString());
                }
            }

            if (arrExJP.Count > 0)
            {
                fExampleLearn fex = new fExampleLearn(arrExJP, arrExVN);
                this.Hide();
                fex.ShowDialog();

                this.Show();
            }
        }
        #endregion
    }
}
